package com.hdoit.apollo.web.stock.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.common.enums.outStock.OutStockType;
import com.hdoit.apollo.logical.stock.model.OutStock;
import com.hdoit.apollo.logical.stock.model.OutStockLogistics;
import com.hdoit.apollo.logical.stock.model.OutStockShow;
import com.hdoit.apollo.logical.stock.service.IOutStockService;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;

@Controller
@RequestMapping(value = "/api/v1/outStock/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/outStock")
public class OutStockApi extends BaseApi{

	@Autowired
	private IOutStockService outStockService;
	
	private final String saveOutStockApi="saveOutStock";
	private final String saveOutStockListApi="saveOutStockList";
	
    @RequestMapping(value = saveOutStockApi)
    @ApiOperation(notes = saveOutStockApi, httpMethod = "POST", value = "保存出库信息")
    public @ResponseBody String saveOutStock(OutStock outStock,String outStockShowsJson,String outStockLogisticsJson){
    	OutStock outStockPan = outStockService.queryModel(outStock.getOutStockId());
    	if(outStockPan==null){
    		outStock.setCreateDate(DateUtils.getCurrent());
    		outStock.setCreateUserId(getLoginId());
    		outStock.setCreateUserName(getLoginName());
            outStock.setDeleteMark(YesOrNoInteger.no);
            outStock.setModelStatus(ModelStatus.add);
    	}else{
    		outStock.setModifyUserId(getLoginId());
    		outStock.setModifyUserName(getLoginName());
    		outStock.setModifyDate(DateUtils.getCurrent());
    		outStock.setModelStatus(ModelStatus.update);
    	}
    	
    	@SuppressWarnings("unchecked")
		List<OutStockShow> outStockShows = (List<OutStockShow>) JsonUtils.readValues(outStockShowsJson, List.class, OutStockShow.class);
    	
    	ResponseModel rm=new ResponseModel();
    	if(OutStockType.fahuo.equals(outStock.getType())) {
    		OutStockLogistics outStockLogistics= (OutStockLogistics) JsonUtils.readValue(outStockLogisticsJson,  OutStockLogistics.class);
        	if(!outStockService.applyOutStockFahuo(outStock,outStockShows,outStockLogistics)){
        		rm.setStatus(ResponseStatus.dataError);
        	}
    	}else {
        	if(!outStockService.applyOutStock(outStock,outStockShows)){
        		rm.setStatus(ResponseStatus.dataError);
        	}
    	}

    	return rm.toJson();
    }
    
    @RequestMapping(value = saveOutStockListApi)
    @ApiOperation(notes = saveOutStockListApi, httpMethod = "POST", value = "批量发货信息信息")
    public @ResponseBody String saveOutStockList(String billingsJson,String outStockLogisticsJson){
        
    	@SuppressWarnings("unchecked")
		List<Billing> billings = (List<Billing>) JsonUtils.readValues(billingsJson, List.class, Billing.class);
    	
    	ResponseModel rm=new ResponseModel();
		OutStockLogistics outStockLogistics= (OutStockLogistics) JsonUtils.readValue(outStockLogisticsJson,  OutStockLogistics.class);
    	if(!outStockService.applyOutStockFahuoList(billings,outStockLogistics,getLoginId(),getLoginName())){
    		rm.setStatus(ResponseStatus.dataError);
    	}
  
    	return rm.toJson();
    }

}